package com.bblocks.common.anti_reptile.config;

import com.bblocks.common.anti_reptile.interceptor.AbstractAntiInterceptor;
import com.bblocks.common.anti_reptile.interceptor.AntiReptileInterceptor;
import com.bblocks.common.anti_reptile.rule.AntiReptileRule;
import com.bblocks.common.anti_reptile.rule.RuleActuator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jin
 * @since 2024/3/24
 */
//@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    private final List<AbstractAntiInterceptor> antiInterceptors;

    public WebMvcConfig(List<AbstractAntiInterceptor> antiInterceptors) {
        this.antiInterceptors = antiInterceptors;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        this.antiInterceptors.stream()
                .sorted(Comparator.comparingInt(AbstractAntiInterceptor::getOrder))
                .forEach(i->{
                    log.info("add interceptor:{}", i.getClass().getSimpleName());
                    registry.addInterceptor(i).addPathPatterns("/**");
                });

    }
}
